package com.ptbuy.db.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.ptbuy.base.BaseApplication;
import com.ptbuy.db.pojo.ExportPojo;
import com.ptbuy.utils.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ExportDao {
    public SQLiteDatabase sqLiteOpenHelper;

    public ExportDao() {
        sqLiteOpenHelper = BaseApplication.getDb();
    }


    /**
     * 增加计数信息
     */
    public synchronized int addInfo(String exportFilePath,String clientid) {
        sqLiteOpenHelper.beginTransaction();
        try {
            Date date = new Date();
            String currentTime = Utils.getCurrentTime(date);
            String s = Utils.stringToTime(currentTime);
            ContentValues values = new ContentValues();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            values.put("id", uuid);
            values.put("exportDate", s);
            values.put("exportFilePath", exportFilePath);
            values.put("clientid", clientid);
            long insertCount = sqLiteOpenHelper.insert("export", null, values);
            sqLiteOpenHelper.setTransactionSuccessful();
        } finally {
            sqLiteOpenHelper.endTransaction();
        }
        return 1;
    }


    /**
     * 查询计录信息
     */
    public synchronized List<ExportPojo> queryExportPojo() {
        List<ExportPojo> list = new ArrayList<>();
        try {
            String sql = "select id,exportFilePath,exportDate,clientid  from export ";
            Cursor cursor = null;

            cursor = sqLiteOpenHelper.rawQuery(sql, null);
            ExportPojo recodInfo = null;
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ExportPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setExportFilePath(cursor.getString(cursor.getColumnIndex("exportFilePath")));
                    recodInfo.setExportDate(cursor.getString(cursor.getColumnIndex("exportDate")));
                    recodInfo.setClientid(cursor.getString(cursor.getColumnIndex("clientid")));
                    list.add(recodInfo);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }


    /**
     * 查询计录信息
     */
    public synchronized ExportPojo queryExportPojoByClientid(String clientid) {
        ExportPojo recodInfo = null;
        try {
            String sql = "select id,exportFilePath,exportDate,clientid  from export where clientid=?";
            Cursor cursor = null;

            cursor = sqLiteOpenHelper.rawQuery(sql, new String[]{clientid});

            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ExportPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setExportFilePath(cursor.getString(cursor.getColumnIndex("exportFilePath")));
                    recodInfo.setExportDate(cursor.getString(cursor.getColumnIndex("exportDate")));
                    recodInfo.setClientid(cursor.getString(cursor.getColumnIndex("clientid")));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return recodInfo;
    }


    public synchronized int delInfo(String id) {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("export", "id=?", new String[]{id});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }

    public synchronized int delInfoByclientid(String clientid) {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("export", "clientid=?", new String[]{clientid});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }
}
